var wxRequest = require('../../utils/wxRequest')
var Common = require('../../utils/common');
const app = getApp();
Page({
  setdata: app.setdata,
  input: app.input,
  input1: function (e) {

    var obj = {};
    obj[e.currentTarget.dataset.name] = e.detail.value.toUpperCase();
    this.setData(obj);

    console.log(obj)
  },
  /**
   * 页面的初始数据
   */
  data: {
    input_status:0,
    phone:"",
    name:"",
    des:"",
    carNum: "",
    month: 3,

    parkingFeeProject:[],

    nameCommunity: "",
    communityAreaNameList: [],
    communityAreaIdList: [],
    communityAreaName: "请选择",
    communityAreaId: 0,
    communityArea_pickerDisabled: true,

    communityFloorNameList: [],
    communityFloorIdList: [],
    communityFloorName: "请选择",
    communityFloorId: 0,
    communityFloor_pickerDisabled: true,

    communityRoomNameList: [],
    communityRoomIdList: [],
    communityRoomName: "请选择",
    communityRoomId: 0,
    communityRoom_pickerDisabled: true,

    mrRoomFlag: 0,//常住房间
    identityType: 0,//身份属性

    city_name: "琼",
    citys: [
      "京",
      "津",
      "冀",
      "晋",
      "蒙",
      "辽",
      "吉",
      "黑",
      "沪",
      "苏",
      "浙",
      "皖",
      "闽",
      "赣",
      "鲁",
      "豫",
      "鄂",
      "湘",
      "粤",
      "桂",
      "琼",
      "渝",
      "川",
      "黔",
      "滇",
      "藏",
      "陕",
      "甘",
      "青",
      "宁",
      "新",
      "台",
      "港",
      "澳",
    ],

    idParkingFeeProject:"",


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.init(this);

    app.api.client_parkingFee_getParkingFeeProjects({
      token: wx.getStorageSync('token'),
      idCommunity: wx.getStorageSync('idCommunity'),
    }, (res) => {

      this.setData({
        parkingFeeProject: res.data
      })
      if (res.status == -1) {
        app.toast(res.msg);
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.init(this);

    
    var that = this;
   


    that.setData({
      nameCommunity: wx.getStorageSync('nameCommunity')
    });
    var url = app.globalData.domain + "/client/community/getAreaByCommunity";
    var parm = {
      idCommunity: wx.getStorageSync('idCommunity'),
      token: wx.getStorageSync("token")
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("获取小区的区域=>", res);
      var communityAreaNameList = [];
      var communityAreaIdList = [];
      var communityArea_pickerDisabled = true;
      if (res.data.length > 0) {
        for (var index in res.data) {
          communityAreaNameList[index] = res.data[index].name;
          communityAreaIdList[index] = res.data[index].id;
        }
        communityArea_pickerDisabled = false;
      }
      that.setData({
        communityAreaNameList: communityAreaNameList,
        communityAreaIdList: communityAreaIdList,
        communityArea_pickerDisabled: communityArea_pickerDisabled
      });
      console.log("communityList", communityList);
    }, error => {
      Common.showToast(error);
    });
  },
  //选择小区域
  communityAreaListChange: function (event) {
    var that = this;
    console.log("communityListValue", event);
    that.setData({
      communityAreaName: that.data.communityAreaNameList[event.detail.value],
      communityAreaId: that.data.communityAreaIdList[event.detail.value]
    });
    var url = app.globalData.domain + "/client/community/getFloorByArea";
    var parm = {
      idArea: that.data.communityAreaId,
      token: wx.getStorageSync("token")
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("获取区域的楼宇=>", res);
      var communityFloorNameList = [];
      var communityFloorIdList = [];
      var communityFloor_pickerDisabled = true;
      if (res.data.length > 0) {
        for (var index in res.data) {
          communityFloorNameList[index] = res.data[index].name;
          communityFloorIdList[index] = res.data[index].id;
          console.log("communityFloorNameList", index);
        }
        communityFloor_pickerDisabled = false;
      }
      that.setData({
        communityFloorNameList: communityFloorNameList,
        communityFloorIdList: communityFloorIdList,
        communityFloor_pickerDisabled: communityFloor_pickerDisabled
      });
      console.log("communityFloorNameList", communityFloorNameList);
    }, error => {
      Common.showToast(error);
    });
  },
  //选择栋
  communityFloorListChange: function (event) {
    var that = this;
    console.log("communityListValue", event);

    that.setData({
      communityFloorName: that.data.communityFloorNameList[event.detail.value],
      communityFloorId: that.data.communityFloorIdList[event.detail.value]
    });
    var url = app.globalData.domain + "/client/community/getRoomByFloor";
    var parm = {
      idFloor: that.data.communityFloorId,
      token: wx.getStorageSync("token")
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("获取楼宇的房屋=>", res);
      var communityRoomNameList = [];
      var communityRoomIdList = [];
      var communityRoom_pickerDisabled = true;
      if (res.data.length > 0) {
        for (var index in res.data) {
          communityRoomNameList[index] = res.data[index].name;
          communityRoomIdList[index] = res.data[index].id;
          console.log("communityRoomNameList", index);
        }
        communityRoom_pickerDisabled = false;
      }
      that.setData({
        communityRoomNameList: communityRoomNameList,
        communityRoomIdList: communityRoomIdList,
        communityRoom_pickerDisabled: communityRoom_pickerDisabled
      });
      console.log("communityRoomNameList", communityRoomNameList);
    }, error => {
      Common.showToast(error);
    });
  },
  //选择室
  communityRoomListChange: function (event) {
    var that = this;
    console.log("communityListValue", event);
    that.setData({
      communityRoomName: that.data.communityRoomNameList[event.detail.value],
      communityRoomId: that.data.communityRoomIdList[event.detail.value]
    });
  },
  //设置默认
  setMrRoom: function (event) {
    if (event.detail.value) {
      this.setData({
        mrRoomFlag: 1
      });
    } else {
      this.setData({
        mrRoomFlag: 0
      });
    }
  },
  //身份属性
  identityTypeChange: function (event) {
    this.setData({
      identityType: event.detail.value
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindPickerChange: function (e) {
    console.log(e)
    this.setData({
      city_name: this.data.citys[e.detail.value]
    })
  },

  bindPickerChange_month: function (e) {
    console.log(e)
    this.setData({
      month: parseInt(e.detail.value) + 1
    })
  },

  idParkingFeeProject: function (e) {
    console.log(e.detail.value)
    this.setData({
      idParkingFeeProject: e.detail.value
    })
  },

  bindblur: function (e) {
    this.setData({
      input_status: 0
    })
  },

  client_parkingFee_apply: function () {

    if (this.data.idParkingFeeProject == ""){
      app.toast("请选择月卡车类型");
      return false;
    }

    let data = {
      token: wx.getStorageSync('token'),
      idCommunity: wx.getStorageSync('idCommunity'),

      phone: this.data.phone,
      name: this.data.name,
      sf: this.data.identityType,
      des: this.data.des,
      idParkingFeeProject: this.data.idParkingFeeProject,
      carNum: this.data.city_name + this.data.carNum,
      applyMonth: this.data.month,

    };

    if (this.data.mrRoomFlag == 0){
      if (this.data.communityRoomId == 0) {
        app.toast("请选择房屋");
        return false;
      }

      data.idRoom = this.data.communityRoomId
    }
    

    console.log( JSON.stringify(data) );
    
    // return false;

    app.api.client_parkingFee_apply(data, (res) => {

      if (res.status == -1) {
        app.toast(res.msg);
      }
      else {
        wx.navigateTo({
          url: '../car_pay/car_pay?data=' + JSON.stringify(data)
            + "&res=" + JSON.stringify(res.data)
        })
      }

    })




  },

})